<?php
/**
 * Created by Sublime.
 * Author: lingyun
 * Date: 2017-12-12 17:15:19
 * @desc 支付宝通知处理类
 */
namespace app\comm\service;
use think\Controller;
use think\Db;
class AlipayService
{
    protected $params; // 订单信息
    /**
     * 回调处理
     * @Author   lingyun
     * @DateTime 2017-12-12T17:26:11+0800
     * @return   [type]                   [description]
     */
    public function init()
    {
        // 1.验签和参数校检
        $result = $this->checkSignAndOrder();
        if($result['code'] == 1) {
            $result = $this->orderHandle();
            if($result['code'] == 0){
                return ['code'=>0,'msg'=>$result['msg']];
            }else{
                return ['code'=>1,'msg'=>$result['msg']];
            }
            // echo "success"; //请不要修改或删除
        }else{
            return ['code'=>0,'msg'=>$result['msg']];
            // echo "fail"; //请不要修改或删除
        }
    }
    /**
     * 更新订单交易结果
     * @Author   lingyun
     * @DateTime 2017-12-12T17:58:57+0800
     * @return   [type]                   [description]
     */
    public function orderHandle()
    {
        $_POST['trade_status'] = 'TRADE_SUCCESS';
        if($_POST['trade_status'] == 'TRADE_SUCCESS') {
            $orderStatus = $this->checkOrderStatus();       //
            if(!$orderStatus) {
                // 判断订单状态, 如果订单未做过处理,则处理订单
                $handlerResult = $this->handle();
                if($handlerResult['code'] == 0) {
                    // 如果订单未处理成功
                    return ['code'=>0,'msg'=>$handlerResult['msg']];
                }else{
                    return ['code'=>1,'msg'=>$handlerResult['msg']];
                }
            }
        }
    }
    /**
     * 验签查询订单状态
     * @Author   lingyun
     * @DateTime 2017-12-12T17:56:16+0800
     * @return   [type]                   [description]
     */
    public function checkSignAndOrder()
    {
        $this->getOrder();
        if(empty($this->params)) {
            return ['code'=>0,'msg'=>'找不到订单信息','data'=>array()];
        }
        $result = \alipay\Notify::check($this->params);
        if(!$result) {
            return ['code'=>0,'msg'=>'校验失败','data'=>array()];
        }
        return ['code'=>1,'msg'=>'支付成功','data'=>array()];
    }
    /**
     * 获取支付订单信息
     * @Author   lingyun
     * @DateTime 2017-12-12T17:52:22+0800
     * @return   [type]                   [description]
     */
    private function getOrder()
    {
        // 以下仅示例
        $out_trade_no = $_POST['out_trade_no'];
        $order = Db::name('order')->where('order_sn',$out_trade_no)->find();
        $params = [
            'out_trade_no' => $order['order_sn'],       //订单号
            'total_amount' => $order['final_amount'],   //交易金额
            'pay_status'   => $order['pay_status'],     //订单支付状态
            'id'           => $order['id'],             //订单id
            'pay_type'     => $order['pay_type'],       //支付方式
            'uid'          => $order['uid'],            //下单人
            'order_type'   => $order['order_type'],     //订单类型，1线上，0线下
            'storeid'      => $order['store_id']        //门店id
        ];
        $this->params = $params;
    }
    /**
     * 判断订单是否已经处理
     * @Author   lingyun
     * @DateTime 2017-12-12T18:00:30+0800
     * @param    [type]                   $params [description]
     * @return   [type]                           [description]
     */
    private function checkOrderStatus(){
        if($this->params['pay_status'] == 0) {
            return false;
        }else{
            return true;
        }
    }
    /**
     * 处理订单状态
     * @Author   lingyun
     * @DateTime 2017-12-12T18:06:17+0800
     * @return   [type]                   [description]
     */
    private function handle()
    {
        // 以下仅示例
        $result = Db::name('order')->where('id', $this->params['id'])->update(['pay_status'=>1,'pay_code'=>$_POST['trade_no'],'pay_time'=>time()]);
        if($result){
            $res = Db::name('pay_log')->insert(['order_id'=>$this->params['id'],'order_sn'=>$this->params['out_trade_no'],'order_type'=>$this->params['order_type'],'uid'=>$this->params['uid'],'pay_type'=>$this->params['pay_type'],'pay_sign'=>$this->params['pay_sign'],'amount'=>$this->params['total_amount'],'addtime'=>time()]);
            if($res>0){
                $res = Db::name('order_action')->insert(['uid'=>$this->params['uid'],'order_id'=>$this->params['id'],'action_user'=>0,'order_status'=>1,'pay_status'=>1,'status_desc'=>'订单已付款，等待发货','storeid'=>$this->params['storeid']]);
                if($res>0){
                    return ['code'=>1,'msg'=>'保存订单动作成功','data'=>array()];
                }else{
                    return ['code'=>0,'msg'=>'保存订单状态记录失败','data'=>array()];
                }
            }else{
                return ['code'=>0,'msg'=>'保存支付记录失败','data'=>array()];
            }
        }else{
            return ['code'=>0,'msg'=>'更新订单状态失败','data'=>array()];
        }
    }
}